﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Data;

public partial class Admin_Default2 : System.Web.UI.Page
{
    clsDBOperation objDBOp = new clsDBOperation();
    Hashtable hstParameters = new Hashtable();
    DataSet dsadmin = new DataSet();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["dsGolbal"] == null)
        {
            Response.Redirect("../");
        }
        dsadmin = (DataSet)Session.Contents["dsGolbal"];
        if (!IsPostBack)
        {
            GetCategory(ddlCategory);
        }

        btnAddSubCategory.Attributes.Add("onclick", " return ValidateAddSubCategory(" + txtSubCategory.ClientID + "," + ddlCategory.ClientID + ")");
    }

    private void GetCategory(DropDownList dropDownList)
    {
        try
        {
            DataSet dssave = new DataSet();
            hstParameters.Clear();
            hstParameters.Add("Action", "Get_Category");
            dssave = (DataSet)objDBOp.GetDataset("sp_SubCategory", CommandType.StoredProcedure, hstParameters);
            if (dssave.Tables.Count > 0 && dssave.Tables[0].Rows.Count > 0)
            {

                dropDownList.DataTextField = "CategoryName";
                dropDownList.DataValueField = "CategoryID";
                dropDownList.DataSource = dssave;
                dropDownList.DataBind();
                dropDownList.Items.Insert(0, new ListItem("---Select Category---", "0"));
            }
        }
        catch (Exception ex)
        {
            lblmsg.Text = "An Error Occure " + ex.Message.ToString();
            lblmsg.EnableViewState = false;
        }

    }

    protected void btnAddSubCategory_Click(object sender, EventArgs e)
    {

        Save();
        GetSubCategory(Convert.ToInt16(ddlCategory.SelectedValue.ToString()));

    }

    public void Save()
    {
        try
        {
            int IsSaved;
            hstParameters.Clear();
            hstParameters.Add("Action", "Insert_SubCategory");
            hstParameters.Add("adminid", dsadmin.Tables[0].Rows[0][0].ToString());
            hstParameters.Add("subcategoryname", txtSubCategory.Text.Trim());
            hstParameters.Add("categoryId", Convert.ToInt32(ddlCategory.SelectedValue.ToString()));

            IsSaved = Convert.ToInt32(objDBOp.ExecuteNonQuery("sp_SubCategory ", CommandType.StoredProcedure, hstParameters));
            if (IsSaved > 0)
                lblmsg.Text = "Record Saved Successfully";
            txtSubCategory.Text = "";
        }
        catch (Exception ex)
        {
            lblmsg.Text = "An Error Occure " + ex.Message.ToString();
            lblmsg.EnableViewState = false;
        }
    }


    protected void ddlCategory_SelectedIndexChanged(object sender, EventArgs e)
    {
        GetSubCategory(Convert.ToInt32(ddlCategory.SelectedValue.ToString()));
    }


    private void GetSubCategory(int Categoryid)
    {
        try
        {
            DataSet ds = new DataSet();
            hstParameters.Clear();
            hstParameters.Add("Action", "Get_SubCategory");
            hstParameters.Add("categoryId", Categoryid);
            ds = (DataSet)objDBOp.GetDataset("sp_SubCategory", CommandType.StoredProcedure, hstParameters);
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                grdSubCategory.DataSource = ds;
                grdSubCategory.DataBind();

            }
            else
            {
                grdSubCategory.DataSource = null;
                grdSubCategory.DataBind();
            }
        }
        catch (Exception ex)
        {
            lblmsg.Text = "An Error Occure " + ex.Message.ToString();
            lblmsg.EnableViewState = false;
        }

    }

    protected void grdSubCategory_RowEditing(object sender, GridViewEditEventArgs e)
    {
        grdSubCategory.EditIndex = e.NewEditIndex;
        GetSubCategory(Convert.ToInt32(ddlCategory.SelectedValue.ToString()));
        TextBox txtSubCategoryName = grdSubCategory.Rows[grdSubCategory.EditIndex].FindControl("txtSubCategoryName") as TextBox;
        Label lblCategoryId = grdSubCategory.Rows[grdSubCategory.EditIndex].FindControl("lblCategoryId") as Label;
        DropDownList ddlCategoryName = grdSubCategory.Rows[grdSubCategory.EditIndex].FindControl("ddlCategoryName") as DropDownList;
        GetCategory(ddlCategoryName);
        ddlCategoryName.SelectedValue = lblCategoryId.Text;

        ImageButton imageButton = grdSubCategory.Rows[e.NewEditIndex].Cells[3].Controls[0] as ImageButton;
        imageButton.Attributes.Add("onclick", " return ValidateAddSubCategory(" + txtSubCategoryName.ClientID + "," + ddlCategoryName.ClientID + ")");
    }

    protected void grdSubCategory_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        grdSubCategory.EditIndex = -1;
        GetSubCategory(Convert.ToInt32(ddlCategory.SelectedValue.ToString()));
    }

    protected void grdSubCategory_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int SubCategoryid = Convert.ToInt32(grdSubCategory.DataKeys[e.RowIndex].Value);
        TextBox txtSubCategoryName = grdSubCategory.Rows[e.RowIndex].FindControl("txtSubCategoryName") as TextBox;
        DropDownList ddlCategoryName = grdSubCategory.Rows[e.RowIndex].FindControl("ddlCategoryName") as DropDownList;

        if (txtSubCategoryName != null && ddlCategoryName != null)
            UpdateSubCategory(SubCategoryid, txtSubCategoryName.Text.Trim(), Convert.ToInt32(ddlCategoryName.SelectedValue));
        grdSubCategory.EditIndex = -1;
        GetSubCategory(Convert.ToInt32(ddlCategory.SelectedValue.ToString()));
    }


    private void UpdateSubCategory(int SubCategoryId, string SubCategoryName, int CategoryId)
    {
        try
        {
            hstParameters.Clear();
            hstParameters.Add("Action", "Update_SubCategory");
            hstParameters.Add("subcategoryId", SubCategoryId);
            hstParameters.Add("categoryId", CategoryId);
            hstParameters.Add("subcategoryname", SubCategoryName);
            hstParameters.Add("adminid", dsadmin.Tables[0].Rows[0][0].ToString());

            int intVal = Convert.ToInt32(objDBOp.ExecuteNonQuery("sp_SubCategory", CommandType.StoredProcedure, hstParameters));
            if (intVal > 0)
                lblmsg.Text = "Sub Category Update Successfully!!!";

        }
        catch (Exception ex)
        {
            lblmsg.Text = "An Error Occure " + ex.Message.ToString();
            lblmsg.EnableViewState = false;
        }
    }
}
